package com.twang.stock.service;

import com.twang.stock.common.KaiPanLaCommon;
import com.twang.stock.entity.KaiPanLaBanKuai;
import com.twang.stock.entity.KaiPanLaZhangTingInfo;
import com.twang.stock.entity.KaiPanLaZhiJing;
import com.twang.stock.entity.ex.KaiPanLaZhangTingInfoSeries;
import com.twang.stock.mapper.CompanyCodeInfoMapper;
import com.twang.stock.mapper.KaiPanLaBanKuaiMapper;
import com.twang.stock.mapper.KaiPanLaZhangTingInfoMapper;
import com.twang.stock.mapper.KaiPanLaZhiJingMapper;
import com.twang.stock.redis.RedisUtil;
import org.assertj.core.util.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 开盘啦
 */
@Service
@Slf4j
public class KaiPanLaService {


    public static String KAI_PAN_LA_ZI_JIN_LIU_START_MINUTES="KAI_PAN_LA_ZI_JIN_LIU_START_MINUTES";

    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;

    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private KaiPanLaZhangTingInfoMapper kaiPanLaZhangTingInfoMapper;

    @Resource
    private KaiPanLaZhiJingMapper kaiPanLaZhiJingMapper;

    @Resource
    private KaiPanLaBanKuaiMapper kaiPanLaBanKuaiMapper;


    /**
     * 获取连扳信息
     * @return
     */
    public List<KaiPanLaZhangTingInfoSeries> 获取连扳信息() {
        //获取概念个数
        List<String> 概念个数 = kaiPanLaZhangTingInfoMapper.getZhangTingGaiNian();
        List<KaiPanLaZhangTingInfoSeries> kaiPanLaZhangTingInfoSeriesList=Lists.newArrayList();
        概念个数.forEach(item-> {
            try{
                KaiPanLaZhangTingInfoSeries kaiPanLaZhangTingInfoSeries=new KaiPanLaZhangTingInfoSeries();
                kaiPanLaZhangTingInfoSeries.setName(item);
                List<Integer> data=Lists.newArrayList();
                List<KaiPanLaZhangTingInfo> kaiPanLaZhangTingInfoList = kaiPanLaZhangTingInfoMapper.getZhangTingStockByGaiNian(item);
                List<KaiPanLaZhangTingInfo> 一板=kaiPanLaZhangTingInfoList.stream().filter(stock->stock.getLbs()==1).collect(Collectors.toList());
                KaiPanLaCommon.获取个数(data,一板);
                List<KaiPanLaZhangTingInfo> 二板=kaiPanLaZhangTingInfoList.stream().filter(stock->stock.getLbs()==2).collect(Collectors.toList());
                KaiPanLaCommon.获取个数(data,二板);
                List<KaiPanLaZhangTingInfo> 三板=kaiPanLaZhangTingInfoList.stream().filter(stock->stock.getLbs()==3).collect(Collectors.toList());
                KaiPanLaCommon.获取个数(data,三板);
                List<KaiPanLaZhangTingInfo> 四板=kaiPanLaZhangTingInfoList.stream().filter(stock->stock.getLbs()==4).collect(Collectors.toList());
                KaiPanLaCommon.获取个数(data,四板);
                List<KaiPanLaZhangTingInfo> 更高=kaiPanLaZhangTingInfoList.stream().filter(stock->stock.getLbs()>4).collect(Collectors.toList());
                KaiPanLaCommon.获取个数(data,更高);
                kaiPanLaZhangTingInfoSeries.setData(data);
                kaiPanLaZhangTingInfoSeriesList.add(kaiPanLaZhangTingInfoSeries);
            }catch (Exception e){
                log.error(">>>>>>>>>>> 获取连扳信息 出错 概念={} error={}",item,e );
            }
        });
        return kaiPanLaZhangTingInfoSeriesList;
    }

    /**
     * 获取实时龙虎榜
     */
    public  List<BigDecimal> getKaiPanLaShiShiLongHuBang( StringBuffer stringBuffer,String satrTime,String endTime){
      List<String> dates= kaiPanLaZhiJingMapper.queryDates();
      List<KaiPanLaZhiJing> kaiPanLaZhiJings= kaiPanLaZhiJingMapper.queyByQuJian(dates.get(dates.size()-1),satrTime,endTime);
      List<BigDecimal> amounts=Lists.newArrayList();
      if(CollectionUtils.isEmpty(kaiPanLaZhiJings)){
         List list= Lists.newArrayList(new BigDecimal(0),new BigDecimal(0),new BigDecimal(0),new BigDecimal(0),new BigDecimal(0),new BigDecimal(0),new BigDecimal(0),new BigDecimal(0));
          amounts.addAll(list);
      }else{
          for(int i=kaiPanLaZhiJings.size()-1;i>=0;i--){
              KaiPanLaZhiJing item=kaiPanLaZhiJings.get(i);
              stringBuffer.append("【").append(item.getName()).append("@").append(item.getBk()).append("】");
              amounts.add(item.getZhuLiJingBuy().setScale(2,BigDecimal.ROUND_HALF_UP));
          }
      }
      return amounts;
    }


    /**
     * 生成今日涨停信息
     */
    public void 生成今日涨停信息() {
        for (int i = 1; i <= 5; i++) {
            try {
                List<KaiPanLaZhangTingInfo> list = KaiPanLaCommon.查询涨停原因板块(i);
                kaiPanLaZhangTingInfoMapper.insertBatch(list);
                log.info(">>>>>>>>>>> KaiPanLaService 生成【{}】连板信息完成，个数={}", i, list.size());
            } catch (Exception e) {
                log.error("KaiPanLaService 获取【{}】连板信息出错 e={}", i, e);
            }
        }
    }


    @Transactional
    public void 实时龙虎榜数据(String endMinutes) throws Exception {
        Object obj = redisUtil.get(KAI_PAN_LA_ZI_JIN_LIU_START_MINUTES);
        String startMinutes = (null == obj ? "0925" : obj.toString());
        if (endMinutes.equals("0925")) {
            startMinutes = endMinutes;
            return;
        }
        if (endMinutes.equals("1300")) {
            startMinutes = "1130";
            return;
        }
        log.info(">>>>>>>>>> 初始化开盘啦当日资金流入流出 startMinutes={} endMinutes={}", startMinutes, endMinutes);
        List<KaiPanLaZhiJing> total = com.google.common.collect.Lists.newArrayList();
        //1
        List<KaiPanLaZhiJing> kaiPanLaZhiJingsIn = MinutesChengJIaoeService.getStockMoneyMinutes(startMinutes, endMinutes, MinutesChengJIaoeService.KAI_PAN_LA_ZI_JIN_OREDER_1);
        total.addAll(kaiPanLaZhiJingsIn);
        //2
        List<KaiPanLaZhiJing> kaiPanLaZhiJingsOut = MinutesChengJIaoeService.getStockMoneyMinutes(startMinutes, endMinutes, MinutesChengJIaoeService.KAI_PAN_LA_ZI_JIN_OREDER_0);
        total.addAll(kaiPanLaZhiJingsOut);
        if(!org.springframework.util.CollectionUtils.isEmpty(total)){
            kaiPanLaZhiJingMapper.batchInsert(total);
        }
        redisUtil.set(KAI_PAN_LA_ZI_JIN_LIU_START_MINUTES, endMinutes);
    }


    /**
     * 生成当天的数据板块信息(手动)
     */
    public void createTodayBanKuaiInfobyMinutes(String minutes) {
        try {
            if (minutes.equals("1300")) {
                return;
            }
            List<KaiPanLaBanKuai> kaiPanLaBanKuais = KaiPanLaCommon.getBanKuaiMinutes("", minutes, "0925", minutes, "1");
            if (!org.apache.commons.collections4.CollectionUtils.isEmpty(kaiPanLaBanKuais)) {
                kaiPanLaBanKuaiMapper.batchInsert(kaiPanLaBanKuais);
            }
        } catch (Exception e) {
            log.error(">>>>>>>>>>> 生成当天的数据板块信息 error rEnd={},error={}", minutes, e);
        }
    }





}
